// 什么是路由？路由就是url地址和组件的对应关系

// 1.引入vue-router
import { createRouter, createWebHashHistory } from "vue-router";
import { useAllDateStore } from "@/stores/index.js";
import { storeToRefs } from "pinia";

// 2.定义路由
const routes = [
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/Login.vue"),
    },
    {
        path: "/",
        name: "main",
        component: () => import("@/views/Main.vue"),
        redirect: "/home", //路由重定向
        children: [
            {
                path: "home",
                name: "home",
                component: () => import("@/views/Home.vue"),
            },
            {
                path: "user",
                name: "user",
                component: () => import("@/views/User.vue"),
            },
            {
                path: "mall",
                name: "mall",
                component: () => import("@/views/Mall.vue"),
            },
            {
                path: "page1",
                name: "page1",
                component: () => import("@/views/Page1.vue"),
            },
            {
                path: "page2",
                name: "page2",
                component: () => import("@/views/Page2.vue"),
            },
        ],
    },
];

// 创建路由实例
const router = createRouter({
    // 设置路由模式, hash模式
    history: createWebHashHistory(),
    routes, // 注入路由配置
});

// // 导航守卫
// router.beforeEach((to, from, next) => {
//     const store = useAllDateStore(); // 获取 store 实例
//     const { token } = storeToRefs(store); // 获取 token
//     // 如果用户访问的是登录页，直接放行
//     if (to.path === "/login") {
//         next();
//     } else {
//         // 如果用户访问的不是登录页，判断 token 是否存在
//         if (token.value) {
//             // 如果 token 存在，放行
//             next();
//         } else {
//             // 如果 token 不存在，跳转到登录页
//             next("/login");
//         }
//     }
// });

// 3.导出路由
export default router;